﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Connectivity;
using Windows.Storage;
using Windows.UI.Notifications;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Weather
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        /// <summary>
        /// 城市
        /// </summary>
        private string city = "北京";

        /// <summary>
        /// 本地设置
        /// </summary>
        ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;

        Weather.Data.Weather weather = (Application.Current as App).weather;

        public MainPage()
        {
            this.InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
             //TODO检查网络
            string connectionProfileInfo = string.Empty;
            //网络信息
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            //根据网络显示
            if (InternetConnectionProfile == null)
            {
                day0Weather.Visibility = Visibility.Collapsed;
                day1Weather.Visibility = Visibility.Collapsed;
                day2Weather.Visibility = Visibility.Collapsed;
                day3Weather.Visibility = Visibility.Collapsed;

                await new MessageDialog("请确保设备已经连接到网络！", "网络异常").ShowAsync();
                 
            }
            else
            {
                day0Weather.Visibility = Visibility.Visible;
                day1Weather.Visibility = Visibility.Visible;
                day2Weather.Visibility = Visibility.Visible;
                day3Weather.Visibility = Visibility.Visible;
                if (localSettings.Values.ContainsKey(city))
                {
                    DayWeather(localSettings.Values[city].ToString());
                }
                else
                    DayWeather(city);
                //pageTitle.Text = city;      
                Titles();//动态瓷贴
            }
        }



        private void DayWeather(string str)
        {
            day0Weather.City = str;
            day1Weather.City = str;
            day2Weather.City = str;
            day3Weather.City = str;
            //this.dataPackageTitle = day0Weather.dataPackageTitle;
            // this.dataPackageDescription =day0Weather.dataPackageDescription;
            // this.dataPackageText = day0Weather.dataPackageText;
        }

        /// <summary>
        /// Live Titles 动态瓷贴
        /// </summary>
        private void Titles()
        {
            XmlDocument titlexml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideSmallImageAndText02);
            XmlNodeList titleTextAttributes = titlexml.GetElementsByTagName("text");

            //for (int i = 0; i < titleTextAttributes.Length; i++)
            //{
            //    string titleText = String.Empty;
            //    titleText = "Text text" + i;
            //    titleTextAttributes.Item((uint)i).AppendChild(titlexml.CreateTextNode(titleText));

            //}
            if (weather != null)
            {
                titleTextAttributes.Item((uint)0).AppendChild(titlexml.CreateTextNode(weather.city));
                titleTextAttributes.Item((uint)1).AppendChild(titlexml.CreateTextNode("白天" + weather.temperature1 + "℃"));
                titleTextAttributes.Item((uint)2).AppendChild(titlexml.CreateTextNode("夜间" + weather.temperature2 + "℃"));
                titleTextAttributes.Item((uint)3).AppendChild(titlexml.CreateTextNode(weather.gm_l));
                titleTextAttributes.Item((uint)4).AppendChild(titlexml.CreateTextNode(weather.gm_s));
            }

            XmlDocument titlexml1 = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideImage);
            XmlNodeList imageElements = titlexml.GetElementsByTagName("image");
            for (int i = 0; i < imageElements.Length; i++)
            {
                XmlElement imageElement = (XmlElement)imageElements.Item((uint)i);
                string imageSource = String.Empty;
                imageSource = "ms-appx:///Images/Logo.png";
                imageElement.SetAttribute("src", imageSource);
            }

            XmlElement bingdingElement = (XmlElement)titlexml.GetElementsByTagName("binding").Item(0);
            bingdingElement.SetAttribute("branding", "None");

            TileNotification tileNotification = new TileNotification(titlexml);


            // TileNotification tileNotification1 = new TileNotification(titlexml1);
            //tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(5);
            TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
            // TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification1);

            //XmlDocument titlexml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText07);
            //XmlNodeList titleTextAttributes = titlexml.GetElementsByTagName("text");
            //for (int i = 0; i < titleTextAttributes.Length; i++)
            //{
            //    string titleText = String.Empty;
            //    titleText = "Text text" + i;
            //    titleTextAttributes.Item((uint)i).AppendChild(titlexml.CreateTextNode(titleText));

            //}
            //if (weather != null)
            //{
            //    titleTextAttributes.Item((uint)0).AppendChild(titlexml.CreateTextNode(weather.city));
            //    titleTextAttributes.Item((uint)1).AppendChild(titlexml.CreateTextNode("白天" + weather.temperature1 + "℃"));
            //    titleTextAttributes.Item((uint)2).AppendChild(titlexml.CreateTextNode("夜间" + weather.temperature2 + "℃"));
            //    titleTextAttributes.Item((uint)3).AppendChild(titlexml.CreateTextNode(weather.gm_l));
            //    titleTextAttributes.Item((uint)4).AppendChild(titlexml.CreateTextNode(weather.gm_s));
            //}
            //XmlNodeList imageElements = titlexml.GetElementsByTagName("image");
            //XmlElement imageElement = (XmlElement)imageElements.Item((uint)0);
            //string imageSoure = "ms-appx:///Images/tile-sdk.png";
            //imageElement.SetAttribute("src", imageSoure);

            //XmlElement bingdingElement = (XmlElement)titlexml.GetElementsByTagName("binding").Item(0);
            //bingdingElement.SetAttribute("branding", "None");

            //TileNotification tileNotification = new TileNotification(titlexml);
            //TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if ((Application.Current as App).City != null)
            {
                city = (Application.Current as App).City;
                localSettings.Values[city] = city;
            }
            pageTitle.Text = city; 
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnCity_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(CityPage));
        }
    }
}
